package com.byl.common.modules.controller;

import com.byl.sdk.captcha.Captcha;
import com.byl.sdk.captcha.ChineseCaptcha;
import com.byl.sdk.captcha.SpecCaptcha;
import com.byl.sdk.captcha.utils.CaptchaUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.OutputStream;

/**
 * Created by Administrator on 2019/7/29.
 */
@Controller
@RequestMapping("/api")
public class CaptchaController {
    @RequestMapping("/images/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {

        OutputStream outputStream = response.getOutputStream();
        // 三个参数分别为宽、高、位数
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 42, 5);
        // 设置字体
        specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32));  // 有默认字体，可以不用设置
        // 设置类型，纯数字、纯字母、字母数字混合
        specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
        // 生成的验证码
        String code = specCaptcha.text();
        //保存验证码
        request.getSession().setAttribute("captcha",code);
        request.getServletContext().setAttribute("captcha",code);
        // 输出图片流
        specCaptcha.out(outputStream);
        outputStream.close();
    }
}
